package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.eduservice.service.feign.VodClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
@Slf4j
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    @Resource
    private VodClient vodClient;
    // 添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo video){
        log.info("数据为::"+video);
        videoService.save(video);
        return R.ok();
    }
    // 删除小节
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id){
        // 首先根据id查询到视频id
        EduVideo video = videoService.getById(id);
        // 得到视频id
        String videoSourceId = video.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.removeVideo(videoSourceId);
        }
        videoService.removeById(id);
        return R.ok();
    }

}

